home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / OpenDoc 1.2b2c1 / Implementation / Memory / MemInit.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-13  |  3.5 KB  |  162 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        MemInit.cpp
  3.  
  4.     Contains:    CFM initializtion for Memory
  5.  
  6.     Owned by:    A. Michael Burbidge
  7.  
  8.     Copyright:    © 1994 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <3>     9/13/96    jpa        1371387: Added CFMTerminate for profiling
  13.                                     purposes.
  14.          <2>     7/11/96    jpa        1364000: Initial heap size = 16k since
  15.                                     system process doesn't need anything
  16.                                     larger.
  17.         <16>      6/7/95    jpa        Removed old (pre-SLIM) SOM includes.
  18.                                     [1256901]
  19.         <15>      6/2/95    TJ        Included Gestalt.h
  20.         <14>      6/1/95    jpa        Restored <12>: Compute total system memory
  21.                                     at init time [1249619]
  22.         <13>     5/17/95    TJ        Backed out changes from previous checkin.
  23.         <11>     1/12/95    jpa        Strings.h --> TextUtils.h [1210936]
  24.         <10>     12/5/94    jpa        Nuked errant pragma lib_export's. [1195676]
  25.          <9>    10/24/94    jpa        Turn off validation
  26.          <8>    10/11/94    NP        1189812: Make Init routine pascal.
  27.          <7>     9/29/94    RA        1189812: Mods for 68K build.
  28.          <6>     9/14/94    jpa        Don't include UseRsrcM.h [1186692]
  29.          <5>      9/9/94    jpa        Added prototype & commented out initBlkPtr
  30.                                     to avoid warnings.
  31.          <4>     8/19/94    jpa        Call ODInitMemory at library init time
  32.                                     [1182106]
  33.          <3>     6/30/94    jpa        Added InitLibraryResources call.
  34.          <2>     6/23/94    NP        Clean up.
  35.          <1>     6/10/94    MB        first checked in
  36.     To Do:
  37.     In Progress:
  38.         
  39. */
  40.  
  41.  
  42. #ifndef __CODEFRAGMENTS__
  43. #include <CodeFragments.h>
  44. #endif
  45.  
  46. #ifndef _MEMMGR_
  47. #include "MemMgr.h"
  48. #endif
  49.  
  50. #ifndef _MEMDEBG_
  51. #include "MemDebg.h"
  52. #endif
  53.  
  54. #ifndef _MEMMGRPV_
  55. #include "MemMgrPv.h"
  56. #endif
  57.  
  58. #ifndef __GESTALT__
  59. #include <Gestalt.h>
  60. #endif
  61.  
  62. #ifndef __ERRORS__
  63. #include <Errors.h>
  64. #endif
  65.  
  66. #ifndef __PROCESSES__
  67. #include <Processes.h>
  68. #endif
  69.  
  70. #ifndef __TEXTUTILS__
  71. #include <TextUtils.h>
  72. #endif
  73.  
  74. #ifndef __SOM__
  75. #include <som.xh>
  76. #endif
  77.  
  78. #if __option(profile)
  79. #include <Profiler.h>
  80. #endif
  81.  
  82.  
  83. mmboolean gHaveSOM;
  84.  
  85. size_t gTotalMemory;                // Declared in PlatfMem.h
  86.  
  87.  
  88. static OSErr
  89. GetProcessName( char name[] )
  90. {
  91.     name[0] = 0;
  92.     
  93.     ProcessSerialNumber psn;
  94.     psn.highLongOfPSN = 0;
  95.     psn.lowLongOfPSN = kCurrentProcess;
  96.     
  97.     ProcessInfoRec info;
  98.     info.processInfoLength = sizeof(info);
  99.     info.processName = (StringPtr) name;
  100.     info.processAppSpec = kMMNULL;
  101.     OSErr err= GetProcessInformation(&psn,&info);
  102.     if( !err )
  103.         p2cstr((StringPtr)name);
  104.     return err;
  105. }
  106.  
  107.  
  108. extern "C" {
  109.     pascal OSErr MemoryCFMInit( CFragInitBlockPtr );
  110.     pascal void  MemoryCFMTerminate( );
  111.     static void* MMCAllocate( size_t size, size_t n );
  112. }
  113.  
  114. static void* MMCAllocate( size_t size, size_t n )
  115. {
  116.     return MMAllocateClearIn(n*size,(MemHeap*)gDefaultHeap);
  117. }
  118.  
  119.  
  120. pascal OSErr MemoryCFMInit( CFragInitBlockPtr )
  121. {
  122. #if MM_DEBUG
  123.     long result;
  124.     if( Gestalt(gestaltLogicalRAMSize,(long*)&gTotalMemory) != noErr )
  125.         gTotalMemory = 0x80000000;    // bogus default value
  126.     if( Gestalt(gestaltVMAttr,&result) == noErr && (result&1) )
  127.         gTotalMemory <<= 1;            // Use double logical RAM size if VM on
  128.                                     // since code fragments get loaded above it
  129. #endif
  130.  
  131.     // Create a default heap:
  132.     char name[256];
  133.     GetProcessName(name);
  134.     strcat(name," default heap"); // For debugging only so hardcoded string is OK
  135.     MemHeap *heap = MMNewHeap(kMMTempMemory,16*1024L,32*1024L,name);
  136.     if( !heap )
  137.         return memFullErr;
  138.     MMSetDefaultHeap(heap);
  139.     
  140.     gHaveSOM = ((void*)&SOMMalloc != (void*)kUnresolvedCFragSymbolAddress);    // Is SOM installed?
  141.  
  142.     if( gHaveSOM ) {
  143.         SOMMalloc = &MMAllocate;
  144.         SOMCalloc = &MMCAllocate;
  145.         SOMRealloc= &MMReallocate;
  146.         SOMFree   = &MMFree;
  147.     }
  148.  
  149. #if __option(profile)
  150.     ProfilerInit(collectDetailed,bestTimeBase,300,30);
  151. #endif
  152.  
  153.     return noErr;
  154. }
  155.  
  156. pascal void MemoryCFMTerminate( )
  157. {
  158. #if __option(profile)
  159.     ProfilerDump("\pMMProfile");
  160. #endif
  161. }
  162.